cmake_minimum_required(VERSION 3.6)

project(CcQtApp)

SET(CMAKE_PREFIX_PATH $ENV{QTDIR})

find_package(Qt5Core $ENV{QTVERSION} REQUIRED)
find_package(Qt5Concurrent $ENV{QTVERSION} REQUIRED)
find_package(Qt5Sql $ENV{QTVERSION} REQUIRED)
find_package(Qt5Gui $ENV{QTVERSION} REQUIRED)
find_package(Qt5Widgets $ENV{QTVERSION} REQUIRED)
find_package(Qt5Network $ENV{QTVERSION} REQUIRED)
find_package(Qt5Multimedia $ENV{QTVERSION} REQUIRED)

set(CMAKE_AUTOMOC ON)                    
set(CMAKE_INCLUDE_CURRENT_DIR ON)

FILE(GLOB_RECURSE FORMS "*.ui")
QT5_WRAP_UI(FORMS_UIC ${FORMS})


file(GLOB_RECURSE All_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/*.mm 
							  ${CMAKE_CURRENT_SOURCE_DIR}/*.m 
							  ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp 
							  ${CMAKE_CURRENT_SOURCE_DIR}/*.mm 
							  ${CMAKE_CURRENT_SOURCE_DIR}/*.c 
							  ${CMAKE_CURRENT_SOURCE_DIR}/*.cc 
							  ${CMAKE_CURRENT_SOURCE_DIR}/*.hh 
							  ${CMAKE_CURRENT_SOURCE_DIR}/*.h 
							  ${CMAKE_CURRENT_SOURCE_DIR}/*.hpp)

cc_source_group_by_dir(All_SOURCES)	

include_directories(${Qt5Core_PRIVATE_INCLUDE_DIRS}
					${Qt5Widgets_PRIVATE_INCLUDE_DIRS}
					${CMAKE_CURRENT_SOURCE_DIR}
					${CMAKE_CURRENT_SOURCE_DIR}/../cc-engine/include
					${CMAKE_CURRENT_SOURCE_DIR}/../cc-tools/include
)


if(WIN32)
	set(exe_property WIN32)
	set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MD /Zi /MP /Gm- /Qpar /wd4996")
	set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MDd")
	set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /DEBUG /OPT:REF /OPT:ICF /LARGEADDRESSAWARE")
	add_definitions(-DUNICODE -D_UNICODE -D_SCL_SECURE_NO_WARNINGS -DWIN32 -DV_LOG -DCURL_STATICLIB)

	link_directories(${CMAKE_CURRENT_BINARY_DIR}/../cc-engine/$(CONFIGURATION)
					 ${CMAKE_CURRENT_BINARY_DIR}/../cc-tools/$(CONFIGURATION)
	)

	set(LINK_LIBRARY CcEngine.lib CcTools.lib)

	# CcEngine.dll路径,到时候要拷贝到可执行程序路径，否则运行.exe会出现CcEngine.dll not found
	set(CCENGINE_PATH ${CMAKE_CURRENT_BINARY_DIR}/../cc-engine/$(Configuration)/CcEngine.dll)
	set(CCTOOLS_PATH ${CMAKE_CURRENT_BINARY_DIR}/../cc-tools/$(Configuration)/CcTools.dll)
elseif(APPLE)
    SET(BUILD_WITH_INSTALL_RPATH TRUE)
    set(exe_property MACOSX_BUNDLE)
    set (CMAKE_CXX_FLAGS "--std=gnu++11 ${CMAKE_CXX_FLAGS}")

	message("CMAKE_OSX_DEPLOYMENT_TARGET is ${CMAKE_OSX_DEPLOYMENT_TARGET}")
	message(${CMAKE_CURRENT_BINARY_DIR})

	set(LINK_LIBRARY libCcEngine.dylib libCcTools.dylib)
	link_directories(${CMAKE_CURRENT_BINARY_DIR}/../cc-engine/$(CONFIGURATION)
		${CMAKE_CURRENT_BINARY_DIR}/../cc-tools/$(CONFIGURATION)
	)

elseif(LINUX)
	SET(CMAKE_BUILD_TYPE Debug)
	SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
endif(WIN32)

set(NAME CcApp)
add_executable(${NAME} ${exe_property} ${All_SOURCES} ${FORMS_UIC} ${MACOSX_BUNDLE_ICON_FILE})

if(WIN32)
	add_custom_command(TARGET CcApp POST_BUILD COMMAND if exist ${CCENGINE_PATH} cmake -E copy ${CCENGINE_PATH} ${CMAKE_CURRENT_BINARY_DIR}/$(Configuration))
	add_custom_command(TARGET CcApp POST_BUILD COMMAND if exist ${CCTOOLS_PATH} cmake -E copy ${CCTOOLS_PATH} ${CMAKE_CURRENT_BINARY_DIR}/$(Configuration))
elseif(APPLE)
	ADD_FRAMEWORK(AVFoundation CcApp)
	ADD_FRAMEWORK(Foundation CcApp)
	ADD_FRAMEWORK(Security CcApp)
	ADD_FRAMEWORK(Carbon CcApp)
	ADD_FRAMEWORK(Appkit CcApp)
	ADD_FRAMEWORK(VideoDecodeAcceleration CcApp)
	ADD_FRAMEWORK(VideoToolbox CcApp)	
	ADD_FRAMEWORK(SystemConfiguration CcApp)	

	set_target_properties(CcApp PROPERTIES XCODE_ATTRIBUTE_DEBUG_INFORMATION_FORMAT "dwarf-with-dsym")
	set_target_properties(CcApp PROPERTIES XCODE_ATTRIBUTE_GCC_GENERATE_DEBUGGING_SYMBOLS "YES")
	set_target_properties(CcApp PROPERTIES XCODE_ATTRIBUTE_SEPARATE_STRIP "YES")
	set_target_properties(CcApp PROPERTIES XCODE_ATTRIBUTE_STRIP_STYLE "Non-Global Symbols")
endif(WIN32)

if(APPLE)
	set_target_properties(${NAME} PROPERTIES XCODE_ATTRIBUTE_ARCHS "x86_64")
endif()

if(WIN32)
	qt5_use_modules(${NAME} WinExtras)
elseif(APPLE)
endif(WIN32)

qt5_use_modules(${NAME} Core)
qt5_use_modules(${NAME} Concurrent)
qt5_use_modules(${NAME} Sql)
qt5_use_modules(${NAME} Gui)
qt5_use_modules(${NAME} Widgets)
qt5_use_modules(${NAME} Network)
qt5_use_modules(${NAME} Multimedia)
qt5_use_modules(${NAME} WebEngineWidgets)
qt5_use_modules(${NAME} WebChannel)

target_link_libraries(${NAME} ${LINK_LIBRARY})
